filetype off
call pathogen#runtime_append_all_bundles()"方便管理plugin
filetype on            " enables filetype detection
filetype plugin on 
filetype plugin indent on

set nocompatible

" Security
set modelines=0

au GUIEnter * simalt ~x "maximum the initial window 

" Tabs/spaces
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

" Soft/hard wrapping
set wrap
set textwidth=79
set formatoptions=qrn1

"use english
set langmenu=en_US
let $LANG = 'en_US'
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

" Basic options
set encoding=utf-8
let &termencoding=&encoding
set fileencodings=utf-8,gbk,ucs-bom,cp936，OK
set ambiwidth=double 
set scrolloff=3
set autoindent
set showmode
set showcmd
set hidden
set wildmenu
set wildmode=list:longest
"set visualbell
set vb t_vb=	"去掉beep声音
set cursorline
set ttyfast
set ruler
set backspace=indent,eol,start
set laststatus=2
set mouse=a  
set nu

" Use the same symbols as TextMate for tabstops and EOLs
set list
set listchars=tab:▸\ ,eol:¬

" Color scheme (terminal)
syntax on
"set background=dark
colorscheme desert

" Save when losing focus
au FocusLost * :wa

" Backups
set backupdir=$VIM/vimfiles/tmp/backup// " backups
set directory=$VIM/vimfiles/tmp/swap//   " swap files
set backup                        " enable backups

" Searching
nnoremap / /\v
vnoremap / /\v
set ignorecase
set smartcase
set incsearch
set showmatch
set hlsearch
set gdefault
map <leader>h :noh<cr>"取消查找后的高亮
runtime macros/matchit.vim
nmap <tab> %
vmap <tab> %

set foldmethod=indent "按缩进自动折叠
"保存自定义的fold
au BufWinLeave * mkview
au BufWinEnter * silent loadview



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""自定义代码
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Python Unittest 的一些设置
" 可以让我们在编写 Python 代码及 unittest 测试时不需要离开 vim
" 键入 :make 或者点击 gvim 工具条上的 make 按钮就自动执行测试用例
"autocmd FileType python compiler pyunit
"autocmd FileType python setlocal makeprg=python\ ./alltests.py
"autocmd BufNewFile,BufRead test*.py setlocal makeprg=python\ %

" 自动使用新文件模板
"autocmd BufNewFile test*.py 0r $VIM/vimfiles/skeleton/test.py
"autocmd BufNewFile alltests.py 0r $VIM/vimfiles/skeleton/alltests.py
autocmd BufNewFile *.py 0r $VIM/vimfiles/skeleton/skeleton.py

"自动加括号
"inoremap ( ()<ESC>i
"inoremap ) <c-r>=ClosePair(')')<CR>
"inoremap { {}<ESC>i
"inoremap } <c-r>=ClosePair('}')<CR>
"inoremap [ []<ESC>i
"inoremap ] <c-r>=ClosePair(']')<CR>

"function ClosePair(char)
	"if getline('.')[col('.') - 1] == a:char
		"return "\<Right>"
	"else
		"return a:char
	"endif
"endf

"python自动加空格
autocmd Filetype python inoremap = <c-r>=EqualSign('=')<CR>
autocmd Filetype python inoremap , ,<space>

function EqualSign(char)
    if getline('.')[col('.') - 3] == a:char
        return "\<ESC>xa".a:char."\<SPACE>"
    else
        return "\<SPACE>".a:char."\<SPACE>\<ESC>a"
    endif
endf

"去掉XPT默认的空格
let g:xptemplate_vars = "SParg="

"let g:equ=1
""设置= + - * 前后自动空格
""逗号后面自动添加空格
"if exists("g:equ")
    ":inoremap = <c-r>=EqualSign('=')<CR>
    ":inoremap + <c-r>=EqualSign('+')<CR>
    "":inoremap - <c-r>=EqualSign('-')<CR>
    "":inoremap * <c-r>=EqualSign('*')<CR>
    "":inoremap / <c-r>=EqualSign('/')<CR>
    ":inoremap > <c-r>=EqualSign('>')<CR>
    ":inoremap < <c-r>=EqualSign('<')<CR>
    ":inoremap , ,<space>
"endif

"function! EqualSign(char)
    "if a:char  =~ '='  && getline('.') =~ ".*("
        "return a:char
    "endif 
    "let ex1 = getline('.')[col('.') - 3]
    "let ex2 = getline('.')[col('.') - 2]

    "if ex1 =~ "[-=+><>\/\*]"
        "if ex2 !~ "\s"
            "return "\<ESC>i".a:char."\<SPACE>"
        "else
            "return "\<ESC>xa".a:char."\<SPACE>"
        "endif 
    "else
        "if ex2 !~ "\s"
            "return "\<SPACE>".a:char."\<SPACE>\<ESC>a"
        "else
            "return a:char."\<SPACE>\<ESC>a"
        "endif 
    "endif
"endf

" 自动更新文件的最后更新时间
function! AutoUpdateTheLastUpdateInfo()
	let s:original_pos = getpos(".")
	let s:regexp = "^\\s*\\([#\\\"\\*]\\|\\/\\/\\)\\s\\?[lL]ast \\([uU]pdate\\|[cC]hange\\):"
	let s:lu = search(s:regexp)
	if s:lu != 0
		let s:update_str = matchstr(getline(s:lu), s:regexp)
		call setline(s:lu, s:update_str . strftime(" %c"))
		call setpos(".", s:original_pos)
	endif
endfunction
"autocmd BufWrite,BufWriteCmd,FileWriteCmd,FileWritePre call AutoUpdateTheLastUpdateInfo()
"autocmd InsertLeave *.{py,c,js,css},*vimrc call AutoUpdateTheLastUpdateInfo()
 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""key map
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Leader
let mapleader = ","

" Use the damn hjkl keys
nnoremap <up> <nop>
nnoremap <down> <nop>
nnoremap <left> <nop>
nnoremap <right> <nop>

" And make them fucking work, too.
nnoremap j gj"可以在自动断行内部移动
nnoremap k gk

" Fuck you, help key.
inoremap <F1> <ESC>
nnoremap <F1> <ESC>
vnoremap <F1> <ESC>

" Faster Esc
inoremap <Esc> <nop>
inoremap jj <ESC>

" Easy buffer navigation
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
map <leader>w <C-w>v<C-w>l ",w打开新的buffer

"fold
nnoremap <space> za

" NERD Tree
map <leader>e :NERDTreeToggle<cr>
let NERDTreeIgnore=['.vim$', '\~$', '.*\.pyc$', 'pip-log\.txt$']

"run python code
nnoremap <leader>q :w<ENTER>:!python %<cr>
"run nose tests
nnoremap <leader>s :w<ENTER>:!nosetests --with-doctest -s<CR> 

"create tags
nnoremap <C-c> :!ctags

"let javaScript_fold=1
let b:javascript_fold=1

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""插件
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Rainbows!
nmap <leader>g :RainbowParenthesesToggle<CR>

"pydiction
let g:pydiction_location = "$VIM/vimfiles/pydiction-1.2/complete-dict"

"TagList
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1

"MiniBufExplorer
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1

"翻译快捷键
nnoremap <leader>f :Trans<CR> 
vnoremap <leader>f <ESC>:TransV<CR> 
